<template>
  <div class="edit-box">
    <el-form :model="form" label-position="top" :inline="true">
      <el-form-item label="预计收款日期">
        <el-date-picker
          v-model="form.receivablesDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          style="width: 100%;"
        />
      </el-form-item>
      <el-form-item label="收款客户">
        <el-select
          v-model="form.customerId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteCustomerList"
          :loading="loading"
        >
          <el-option
            v-for="item in customerOptions"
            :key="item.id"
            :label="item.customerName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联项目">
        <el-select
          v-model="form.projectsId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteProjectList"
          :loading="loading"
        >
          <el-option
            v-for="item in projectList"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联产品|服务">
        <el-select
          v-model="form.productId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteProductList"
          :loading="loading"
        >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.productName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="款项内容">
        <el-input v-model="form.receivablesName"></el-input>
      </el-form-item>
      <el-form-item label="预计收款金额">
        <el-input v-model.number="form.receivablesMoney"></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="leadingId">
        <el-select
          v-model="form.userId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteUserList"
          :loading="loading"
        >
          <el-option
            v-for="item in userOptions"
            :key="item.id"
            :label="item.userName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('changeState',false)">取 消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { findById, update } from '@/api/receive'
import { findByNameLike as findCustomerList } from '@/api/customer'
import { findByNameLike as findProjectList } from '@/api/project'
import { findByNameLike as findProductList } from '@/api/product'
import { getListByName } from '@/api/user'
export default {
  data() {
    return {
      loading: false,
      form: {
        id: '',
        companyId: this.$store.state.user.companyId,
        receivablesName: '',
        productName: '',
        receivablesDate: '',
        receivablesMoney: '',
        customerId: '',
        projectsId: '',
        productId: '',
        state: 1,
        userId: ''
      },
      userOptions: [],
      customerOptions: [],
      productList: [],
      projectList: []
    }
  },
  beforeDestroy() {
    this.$off('reload')
    this.$off('changeState')
  },
  methods: {
    initDialogData(id) {
      this.remoteCustomerList('')
      this.remoteProductList('')
      this.remoteProjectList('')
      this.remoteUserList('')
      findById(id).then(res => {
        this.form = res.data
      })
    },
    // 获取用户列表
    remoteUserList(query) {
      this.loading = true
      let params = { companyId: this.form.companyId, userName: query }
      getListByName(params).then(res => {
        this.loading = false
        this.userOptions = res.data
      })
    },
    // 获取客户列表
    remoteCustomerList(query) {
      this.loading = true
      let params = { companyId: this.form.companyId, customerName: query }
      findCustomerList(params).then(res => {
        this.loading = false
        this.customerOptions = res.data
      })
    },
    // 获取产品列表
    remoteProductList(query) {
      this.loading = true
      let params = { companyId: this.form.companyId, productName: query }
      findProductList(params).then(res => {
        this.loading = false
        this.productList = res.data
      })
    },
    // 获取项目列表
    remoteProjectList(query) {
      this.loading = true
      let params = { companyId: this.form.companyId, projectsName: query }
      findProjectList(params).then(res => {
        this.loading = false
        this.projectList = res.data
      })
    },
    submit() {
      update(this.form).then(res => {
        if (res.success) {
          this.$emit('reload')
          this.$emit('changeState', false)
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.edit-box {
  padding: 0 50px;
}
.dialog-footer {
  text-align: right;
}
.el-form--inline .el-form-item {
  width: calc(50% - 12px);
}
</style>
